package com.cq.androidtools.jsoup

import java.util.regex.Pattern

/**
 * 作者: CQ
 * 日期: 2022-03-09
 * 说明:
 */
fun main() {

    val aaa = "[-]"
    val zkqId = aaa.subSequence(1, aaa.length - 2)
    println(zkqId)
    val bbb = "[dddd-]"
    val zkqId2 = bbb.subSequence(1, bbb.length - 2)
    println(zkqId2)

    val str1 = """<page> <layout><layout a1 = "a11"> <text v1 = "v11"> </text> </layout> <layout a2 = "a22"> <text v2 = "v22"> </text> </layout></layout> </page>""".trimMargin()

    val str2 = """<% var w = 100  var h = 200%>""" + str1

    val pattern = "<%?var.*[%>]{1}"
    val pattern2 = "<%var.*%>"

    println(Pattern.matches(pattern, str2))
    println(Pattern.matches(pattern2, str2))
    val r = Pattern.compile(pattern)
    val m = r.matcher(str2)

    var content = ""

    if (str2.startsWith("<%")) {
        if (str2.contains("%>")) {
            val pos = str2.indexOf("%>")
            println(pos)
            println(str2.substring(0, pos + 2))
            content = str2.substring(pos + 2, str2.length)
            println(str2.substring(pos + 2, str2.length))
        }
    }

    if (content.startsWith("<page>") && content.endsWith("</page>")) {
        content = content.substring(content.indexOf("<page>") + "<page>".length, content.indexOf("</page>"))
        println(content)
    }
    getListIndexOf(content, "<layout")
    getListIndexOf(content,"</layout>")


}

fun getListIndexOf(content : String, string : String){
    var pos = 0
    var index = content.indexOf(string, pos)
    while(index >= 0) {
        pos = index + string.length
        println(index)
        index = content.indexOf(string, pos)
    }

}


